//
//  ProvinceController.m
//  DMGME
//
//  Created by ihope99 on 15/9/8.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "ProvinceController.h"

@interface ProvinceController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ProvinceController
{
    NSMutableArray *arrPrivc;
    NSMutableArray *arrPrivcId;
    UITableView *tableview;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"%@",_provinceTypeId);
    
    [self setTitle:[NSString stringWithFormat:@"%@位置选择",_provinceType]];
    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self creatView];
    if ([_provinceType isEqualToString:@"省"]) {
        [self provinceNet];
    }else if ([_provinceType isEqualToString:@"市"]){
        [self cityNet];
    }else{
        [self cityNet];
    }
}
- (void)cityNet{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$region"] forKey:@"token"];
    [dict setValue: @"region" forKey:@"action"];
    [dict setValue: _provinceTypeId forKey:@"region_id"];
    
    arrPrivc = [NSMutableArray array];
    arrPrivcId = [NSMutableArray array];
    
    [AFDownManager POST:REGISTERNET parameters:dict success:^(id responseObject) {
//        NSLog(@"%@",responseObject);
        
        for (int i = 0; i<=[responseObject[@"data"] count]-1; i++) {
            [arrPrivc addObject:responseObject[@"data"][i][@"region_name"]];
            [arrPrivcId addObject:responseObject[@"data"][i][@"region_id"]];
        }
//        NSLog(@"%@",arrPrivc);
        [tableview reloadData];
        
    } failure:^(NSError *error) {
    }];
}
- (void)provinceNet{
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$province"] forKey:@"token"];
        [dict setValue: @"province" forKey:@"action"];
    
    arrPrivc = [NSMutableArray array];
    arrPrivcId = [NSMutableArray array];
    
        [AFDownManager POST:REGISTERNET parameters:dict success:^(id responseObject) {
//            NSLog(@"%@",responseObject);
           
            for (int i = 0; i<=[responseObject[@"data"] count]-1; i++) {
                [arrPrivc addObject:responseObject[@"data"][i][@"region_name"]];
                [arrPrivcId addObject:responseObject[@"data"][i][@"region_id"]];
            }
//            NSLog(@"%@",arrPrivc);
            [tableview reloadData];
            
        } failure:^(NSError *error) {
        }];
}
- (void)creatView
{
    UILabel *titileble = [[UILabel alloc] init];
    titileble.frame = CGRectMake(15, 15, 150, 13);
    titileble.text = [NSString stringWithFormat:@"选择您的%@位置",_provinceType];
    titileble.textColor = ComColor;
    titileble.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    [self.view addSubview:titileble];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(0, 39, KWidth, 1);
    lineView.backgroundColor = [UIColor grayColor];
    lineView.alpha = 0.4;
    [self.view addSubview:lineView];
    
    tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, KWidth, KHeight - 40) style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:tableview];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrPrivc.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID =@"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID ];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    if ([tableview respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [tableview setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([tableview respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [tableview setLayoutMargins:UIEdgeInsetsZero];
    }
    
        cell.textLabel.text =[NSString stringWithFormat:@"%@",arrPrivc[indexPath.row]];
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    self.ProvinceStr(arrPrivc[indexPath.row],arrPrivcId[indexPath.row]);
    [self.navigationController popViewControllerAnimated:YES];
    
}
//分割线
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
- (void)provinceStr:(void (^)(NSString *, NSString *))blok{
    self.ProvinceStr = blok;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
